package com.dqhl.community.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.arrownock.exception.ArrownockException;
import com.arrownock.social.AnSocialMethod;
import com.arrownock.social.IAnSocialCallback;
import com.dqhl.community.R;
import com.dqhl.community.base.BaseActivity;
import com.dqhl.community.util.Constant;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by cheng on 2016/9/7.
 * 社区医疗 activity
 */
public class HospitalActivity extends BaseActivity implements View.OnClickListener {

    private ImageView ivTopBack;
    private TextView tvTopTitle;

    private RelativeLayout rlOnlineTalk;
    private RelativeLayout rlOnlineOrdered;
    private RelativeLayout rlHealthyFile;
    private String clientId;//clientId
    private String username;//用户名

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hospital);

        initView();
        //TODO 更改用户名：密码；
        String phone = spUserInfo.getString(Constant.ANIM_PHONE, "");
        String pwd = spUserInfo.getString(Constant.ANIM_PWD, "");
        Log.d("HospitalActivity",phone+"："+pwd);
        DengLu(phone,pwd);
//        DengLu("testuser","123456");
    }
    private void DengLu(String name,String pwd){
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("username", name);
        params.put("password", pwd);
        try {
            social.sendRequest("users/auth.json", AnSocialMethod.POST, params,
                    new IAnSocialCallback() {
                        @Override
                        public void onFailure(JSONObject response) {
                            try {
                                toast(R.string.error_net_connection);
                            } catch (Exception e1) {
                            }
                        }
                        @Override
                        public void onSuccess(JSONObject response) {
                            try {
                                Log.d("HospitalActivity",response.getJSONObject("response").getString("user"));
                                clientId = response.getJSONObject("response").getJSONObject("user").getString("clientId");
                                username = response.getJSONObject("response").getJSONObject("user").getString("username");
                                Log.d("onSuccess", clientId);
//                                Log.d("onSuccess", "clientId="+clientId);
                                //将登陆成功的userid保存
                                SharedPreferences.Editor editor = spUserInfo.edit();
                                editor.putString(Constant.IM_CLIENTID,clientId);
                                editor.putString(Constant.IM_USERNAME,username);
                                editor.apply();
                            } catch (Exception e1) {
                                Log.d("onSuccess", "json解析失败");
                            }
                        }
                    });
        } catch (ArrownockException e) {
            e.printStackTrace();
        }
    }

    private void initView() {
        ivTopBack = (ImageView) findViewById(R.id.iv_top_back);
        ivTopBack.setVisibility(View.VISIBLE);
        tvTopTitle = (TextView) findViewById(R.id.tv_top_title);
        tvTopTitle.setText(R.string.tv_hospital);

        rlOnlineTalk = (RelativeLayout) findViewById(R.id.rl_online_talk);
        rlOnlineOrdered = (RelativeLayout) findViewById(R.id.rl_online_ordered);
        rlHealthyFile = (RelativeLayout) findViewById(R.id.rl_healthy_file);

        ivTopBack.setOnClickListener(this);
        rlOnlineTalk.setOnClickListener(this);
        rlOnlineOrdered.setOnClickListener(this);
        rlHealthyFile.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_top_back:
                finish();
                break;
            case R.id.rl_online_talk: //在线咨询
                if (TextUtils.isEmpty(spUserInfo.getString(Constant.IM_CLIENTID,""))){
                    toast("访问异常，请稍后重试...");
                }else {
                    Intent intent = new Intent(HospitalActivity.this, ChatActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.rl_online_ordered: //在线预约
//                overlay(CompleteInfoActivity.class);
                overlay(CommuunityHealth.class);
                break;
            case R.id.rl_healthy_file: //健康档案
                overlay(HealthyFileActivity.class);
                break;
        }
    }
}
